package com.savvy.util;

import java.util.Vector;

import com.savvy.exception.CodeException;

import android.os.Handler;

/**
 * It is a factory class for TimerWithHandler
 * 
 * @author Bennis
 * 
 */
public class TimerWithHandlerFactory {

	private static Vector<TimerWithHandler> timerVector;
	private static int nTimers;

	static {
		timerVector = new Vector<TimerWithHandler>();
		nTimers = 0;
	}

	public static final int create(Handler handler, long interval, int attribute) {
		int timerNo = nTimers++;
		TimerWithHandler hTimer;
		hTimer = new TimerWithHandler(handler, interval, attribute);
		timerVector.add(timerNo, hTimer);
		return timerNo;
	}

	public static final void start(int timerNo) {
		TimerWithHandler hTimer = timerVector.get(timerNo);
		if (!hTimer.equals(null)) {
			try {
				hTimer.start();
			} catch (CodeException e) {
				e.printStackTrace();
			}
		}
	}

	public static final void pause(int timerNo) {
		TimerWithHandler hTimer = timerVector.get(timerNo);
		if (!hTimer.equals(null)) {
			try {
				hTimer.setPause(true);
			} catch (CodeException e) {
				e.printStackTrace();
			}
		}
	}

	public static final void resume(int timerNo) {
		TimerWithHandler hTimer = timerVector.get(timerNo);
		if (!hTimer.equals(null)) {
			try {
				hTimer.setPause(false);
			} catch (CodeException e) {
				e.printStackTrace();
			}
		}
	}

	public static final void kill(int timerNo) {
		TimerWithHandler hTimer = timerVector.get(timerNo);
		if (!hTimer.equals(null)) {
			try {
				hTimer.kill();
			} catch (CodeException e) {
				e.printStackTrace();
			} finally {
				timerVector.remove(timerNo);
			}
		}
	}

}
